package com.ujiuye.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * boolean类型   直接print写回去  不需要jackson进行转化
 */
@WebServlet("/demo11Servlet")
public class Demo11Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

       //将对象转成json类型
        /*User user = new User("zhangsan",123);
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(user);
        System.out.println(json);*/

        //将list集合转成json
        List<User> lists = new ArrayList<>();
        lists.add(new User("zhangsan1",121));
        lists.add(new User("zhangsan2",122));
        lists.add(new User("zhangsan3",123));
        lists.add(new User("zhangsan4",124));

        String json = new ObjectMapper().writeValueAsString(lists);

        System.out.println(json);


        //boolean类型   直接print写回去  不需要jackson进行转化

        resp.getWriter().print(json);

    }
}
